package org.example;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GameUtil {


    /**
     * 洗牌方法
     * currList = [0，1，2，3，5.。。103]
     */
    public static PokeBean[] shuffle() {
        // 洗牌后的数组
        PokeBean[] tempPokeArrayBean = new PokeBean[104];
        // 对应扑克牌下标，当下标列表未空时，那么表示洗牌完成
        List<Integer> currList = new ArrayList<Integer>();
        // 记录洗牌后还存在的下标
        for (int i = 0; i < PokeCreate.pokeBeanStaticArray.length; i++) {
            currList.add(i);
        }
        // 随机从下标集合中拿到下标，再取扑克数组对应的下标对应的扑克牌存入临时数组
        for (int i = 0; i < PokeCreate.pokeBeanStaticArray.length; i++) {
            Random random = new Random();
            // [0,104)
            int curr = random.nextInt(currList.size());
            currList.remove(curr);
            PokeBean pokeBean = PokeCreate.pokeBeanStaticArray[curr];
            tempPokeArrayBean[i] = pokeBean;
        }
        return tempPokeArrayBean;
    }

    /**
     * 发牌
     * [null,null,null,方5...]
     * @param currPokeArray
     * @return {@link PokeBean}
     */
    public static PokeBean dealCards(PokeBean[] currPokeArray) {
        for (int i = 0; i < currPokeArray.length; i++) {
            if (currPokeArray[i] != null) {
                PokeBean pokeBean = currPokeArray[i];
                currPokeArray[i] = null;
                return pokeBean;
            }
        }
        return null;
    }


    /**
     * 计算点数
     * @param pokeBeanList
     * @return {@link Integer}
     */
    public static Integer countPoints(List<PokeBean> pokeBeanList) {
        int sum = 0;
        for (int i = 0; i < pokeBeanList.size(); i++) {
            sum += pokeBeanList.get(i).getNumber();
        }
        return sum;
    }

    public static void showPlayerPoke(Player player) {
        System.out.println(String.format("%s 拥有的扑克牌情况: %s", player.getName(), player.getPokeBeansShow()));
    }

    public static int getMaxScore(Player player) {
        List<Integer> score = player.getScore();
        // 取出最大的分数
        int maxScore = score.get(0);
        for (Integer i : score) {
            if (i > maxScore){
                maxScore = i;
            }
        }
        return maxScore;
    }
}
